﻿using QuickManager.Code.Config;
using QuickManager.Code.Config.Model;
using QuickManager.Code.SiteMgr;
using QuickManager.Code.SiteMgr.Model;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;

namespace QuickManager.Code
{
    /// <summary>
    /// 打包工具外挂
    /// </summary>
    public class PackToolHelper
    {

        [DllImport("user32.dll", EntryPoint = "FindWindow", CharSet = CharSet.Auto)]
        static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
        [DllImport("user32.dll", EntryPoint = "FindWindowEx", CharSet = CharSet.Auto)]
        extern static IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
        [DllImport("User32.dll", EntryPoint = "SendMessage")]
        private static extern int SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, string lParam);

        public static void Start(string siteID, string packToolPath)
        {
            int WM_CLICK = 0x00F5, WM_SETTEXT = 0x000C;
            int TotalWaitCount = 30;    //每个环节最多等待3秒
            int waitCount = 0;

            Process process = new Process();
            process.StartInfo.FileName = packToolPath;
            process.Start();
            process.WaitForExit();

            //版本升级后导致后缀变了
            string[] formClasses = new string[]{
                "30d38e8_r12_ad1",
                "30d38e8_r11_ad1",
                "13a5ba8_r11_ad1"
            };
            for (int i = 0; i < formClasses.Length; )
            {

            查找解决方案选定确认按钮:
                string cls = formClasses[i];
                i = (i == formClasses.Length - 1) ? 0 : (i + 1);

                //等待登录框启动
                Thread.Sleep(100);
                IntPtr loginHandle = FindWindow("WindowsForms10.Window.8.app.0." + cls, "用户登录");
                IntPtr btnOK = FindWindowEx(loginHandle, IntPtr.Zero, null, " 确 定");
                SendMessage(btnOK, WM_CLICK, loginHandle, "OK");


                //查找解决方案选择对话框:
                IntPtr slnHandle = FindWindow("WindowsForms10.Window.8.app.0." + cls, "");
                IntPtr pHandle1 = FindWindowEx(slnHandle, IntPtr.Zero, "WindowsForms10.Window.8.app.0." + cls, "");
                IntPtr pHandle2 = FindWindowEx(pHandle1, IntPtr.Zero, "WindowsForms10.Window.8.app.0." + cls, "");
                IntPtr pHandle3 = FindWindowEx(pHandle2, IntPtr.Zero, "WindowsForms10.Window.8.app.0." + cls, "");
                IntPtr pHandle4 = FindWindowEx(pHandle3, IntPtr.Zero, "WindowsForms10.Window.8.app.0." + cls, "");
                IntPtr pHandle5 = FindWindowEx(pHandle3, pHandle4, "WindowsForms10.Window.8.app.0." + cls, "");
                IntPtr editHandle = FindWindowEx(pHandle5, IntPtr.Zero, "WindowsForms10.EDIT.app.0." + cls, "");

                SiteInfoModel siteInfo = SiteManager.GetSiteInfo(siteID);
                SendMessage(editHandle, WM_SETTEXT, IntPtr.Zero, siteInfo.SlnFile);

                //找到确认按钮
                IntPtr pHandle6 = FindWindowEx(pHandle1, pHandle2, "WindowsForms10.Window.8.app.0." + cls, "");
                IntPtr btnSlnOK = FindWindowEx(pHandle6, IntPtr.Zero, "WindowsForms10.BUTTON.app.0." + cls, " 确 定");
                if (btnSlnOK == IntPtr.Zero)
                {
                    ++waitCount;
                    if (waitCount >= TotalWaitCount)
                    {
                        return;
                    }
                    goto 查找解决方案选定确认按钮;
                }
                waitCount = 0;

                SendMessage(btnSlnOK, WM_CLICK, IntPtr.Zero, "OK");
            }
        }
    }
}
